package cn.globalph.housekeeper.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import cn.globalph.housekeeper.R

class OrderNumberProgress : View{
    private var mPaint:Paint = Paint()
    private var mPath:Path = Path()

    private var number = 0
    private var maxNum = Int.MAX_VALUE
    private  var mBitmap:Bitmap? = null

    private  var grayColor:Int = resources.getColor(R.color.gray_ef)
    private  var textGrayColor:Int = resources.getColor(R.color.gray_cc)

    private var textSize:Float = dp2px(17f).toFloat()

    constructor(context: Context):this(context,null)

    constructor(context:Context,attributeSet: AttributeSet?):super(context,attributeSet){
        mPaint.isAntiAlias = true
        mPaint.style = Paint.Style.FILL_AND_STROKE
        mPaint.color = Color.BLACK

        var ta = context.obtainStyledAttributes(attributeSet, R.styleable.OrderNumberProgress)
        maxNum = ta.getInt(R.styleable.OrderNumberProgress_max_num,Int.MAX_VALUE)
        number = ta.getInt(R.styleable.OrderNumberProgress_number,0)

        mBitmap = BitmapFactory.decodeResource(resources,R.drawable.ic_progross_middle)

    }


    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        if(width ==0 || height ==0 || mBitmap == null) return

        val bw = mBitmap!!.width

        val length = bw/3.toFloat()


        mPath.reset()
        mPath.moveTo(bw/2+length/2,length)
        val realW = width-bw-length
        val middle = bw/2+length/2+realW*number.toFloat()/maxNum
        mPath.lineTo(middle,length)
        mPath.lineTo(middle,2*length)
        mPath.lineTo(bw/2+length/2,2*length)

        mPath.arcTo(RectF(bw/2.toFloat(),length,bw/2+length,2*length),90f,180f)
        mPath.close()

        mPaint.shader = LinearGradient(
            0f, 0f, middle, 0f,
            Color.rgb(255, 154, 11), Color.rgb(250, 91, 3), Shader.TileMode.CLAMP
        )

        canvas?.drawPath(mPath,mPaint)

        mPath.reset()
        mPath.moveTo(middle,length)
        mPath.lineTo(width-length/2-bw/2,length)
        mPath.arcTo(RectF(width-length-bw/2,length,width.toFloat()-bw/2,2*length),-90f,180f)
        mPath.lineTo(middle,2*length)
        mPath.close()

        mPaint.shader = null
        mPaint.color = grayColor
        canvas?.drawPath(mPath,mPaint)

        canvas?.drawBitmap(mBitmap!!,middle-mBitmap!!.width/2,0f,mPaint)


        mPaint.color = textGrayColor
        mPaint.textSize = textSize
        var tPadding = 5*length
        var rPadding = 4*length
        canvas?.drawText("0",bw/2.toFloat(),tPadding,mPaint)
        canvas?.drawText((maxNum/3).toString(),bw/2+(width-rPadding-bw)/3.toFloat(),tPadding,mPaint)
        canvas?.drawText((maxNum*2/3).toString(),bw/2+(width-rPadding-bw)*2/3.toFloat(),tPadding,mPaint)
        canvas?.drawText((maxNum).toString(),width.toFloat()-rPadding-bw/2,tPadding,mPaint)
    }

    fun dp2px(dpVal: Float): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpVal,
            resources.displayMetrics
        ).toInt()
    }
}